//
//  TabBarViewController.m
//  TraderMobileKYi
//
//  Created by shijiezhao on 9/6/17.
//  Copyright © 2017年 com.yeezsoft.init. All rights reserved.
//

#import "EBMTabBarViewController.h"
#import "EBMBaseViewController.h"
#import "EBMJumpClass.h"
#import "SJNetStatusTool.h"

@interface EBMTabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation EBMTabBarViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建视图
    [self initView];
    
    //  监听网络变化
    [self notifNetWorkChange];
    
    
    self.delegate = self;
}



/**
 创建视图
 */
- (void)initView
{
    
    // 设置属性
    [self settingAttribute];
    
    
    //  创建控件    创建视图
    [self createViewInVCs];
    
}

// 设置属性
- (void)settingAttribute
{
    self.tabBar.tintColor = RGBCOLOR16_1c1e24;
    self.tabBar.translucent = NO;

    [self.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10], NSForegroundColorAttributeName:RGBCOLOR16_888888} forState:UIControlStateNormal];
    [self.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10], NSForegroundColorAttributeName: RGBCOLOR16_1c1e24} forState:UIControlStateSelected];
}


//  创建控件    创建视图
- (void)createViewInVCs
{
    
    //  首页
    self.homeVC = [[EBMHomeViewController alloc]init];
    //  债券
    self.bondVC = [[EBMBaseViewController alloc]init];
    //  银河间
    self.communityVC = [[EBMBaseViewController alloc]init];
    //  购物车
    self.shopVC = [[EBMBaseViewController alloc]init];
    //  我的
    self.myselfVC = [[EBMBaseViewController alloc]init];
    
    


    // 视图数组
    NSArray *VCArray ;
    // 图片数组
    NSArray *noSelectImageArray;
    NSArray *selectImageArray;
    // 名称
    NSArray *nameArray;
    
    
    // 视图数组
    VCArray = @[self.homeVC,
                self.bondVC,
                self.communityVC,
                self.shopVC,
                self.myselfVC];
    // 图片数组
    noSelectImageArray = @[@"Tabbar_Home_Normal",
                           @"Tabbar_Optional_Normal",
                           @"Tabbar_Trade_Normal",
                           @"Tabbar_Community_Normal",
                           @"Tabbar_Myself_Normal"];
    selectImageArray = @[@"Tabbar_Home_Selected",
                         @"Tabbar_Optional_Selected",
                         @"Tabbar_Trade_Selected",
                         @"Tabbar_Community_Selected",
                         @"Tabbar_Myself_Selected"];
    // 名称
    nameArray = @[@"首页",@"自选",@"交易",@"社区",@"我的"];
    
    
    // 存放Navigation我数组
    NSMutableArray *navigArray = [NSMutableArray array];
    
    for (int i = 0 ; i < VCArray.count; i++)
    {
        UINavigationController *navig = [[EBMBaseNavigationController alloc]initWithRootViewController:VCArray[i]];
        navig.tabBarItem.image = [UIImage imageNamed:noSelectImageArray[i]];
//        navig.tabBarItem.selectedImage = [UIImage imageNamed:selectImageArray[i]];
        navig.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageArray[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navig.tabBarItem.title = nameArray[i];
        
        [navigArray addObject:navig];
        
        
    }
    
    self.viewControllers = navigArray;
}


#pragma mark --------------方法--------------



//  监听网络变化
- (void)notifNetWorkChange
{
    
    //  网络请求    网络变化
    [SJNetStatusTool netWorkChangeEventWithNetworkChangeBlock:^(NSInteger status) {
        
        if(status == 0)
        {
            //  无网络
            
            [MBProgressHUDInLoading MBProgressHUDInTitleWithTitle:HttpUrlError];
        }
        else if (status == 1)
        {
            //  有网络
            
            UIWindow *keyWindow = KWindow;
            
            EBMTabBarViewController *tabarVC = (EBMTabBarViewController *)keyWindow.rootViewController;
            
            UINavigationController *navig = tabarVC.viewControllers[tabarVC.selectedIndex];
            
            EBMBaseViewController *baseVC = navig.viewControllers.lastObject;
            
            if([baseVC isKindOfClass:[EBMBaseViewController class]])
            {
                
                if([baseVC respondsToSelector:@selector(baseRefreshRequestData)])
                {
                    //  刷新界面数据
                    [baseVC baseRefreshRequestData];
                    
                }
                
            }
            
        }
        
    }];
     
    
}


//  按压的抖动效果
- (void)pressQuiveringEffect
{
    
    UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
    if (@available(iOS 13.0, *)) {
        [feedBackGenertor impactOccurredWithIntensity:0.6];
    } else {
        // Fallback on earlier versions
    }
    
}

#pragma mark ----------UITabBarController 代理方法----------

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

    //  按压的抖动效果
    [self pressQuiveringEffect];
    
}


@end
